package com.fenghongzhang.kuaishou.mvp.login.view;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import com.fenghongzhang.kuaishou.MainActivity;
import com.fenghongzhang.kuaishou.R;
import com.fenghongzhang.kuaishou.mvp.login.contract.LoginContract;
import com.fenghongzhang.kuaishou.mvp.login.entity.UserInfoBean;
import com.fenghongzhang.kuaishou.mvp.login.model.LoginModel;
import com.fenghongzhang.kuaishou.mvp.login.presenter.LoginPresenter;
import com.fenghongzhang.kuaishou.mvp.register.view.RegisterActivity;
import com.fenghongzhang.kuaishou.pay.PayActivity;
import com.fenghongzhang.kuaishou.utils.SPUtil;
import com.fenghongzhang.mvp_lib.view.BaseActivity;

import java.security.SecureRandom;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;


public class LoginActivity extends BaseActivity<LoginPresenter> implements LoginContract.ILoginView {

    private EditText name;
    private EditText pwd;
    private Switch remark;
    private Button login;
    private Button goRegister;


    @Override
    public int bindLayout() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        name = (EditText) findViewById(R.id.name);
        pwd = (EditText) findViewById(R.id.pwd);
        remark = (Switch) findViewById(R.id.remark);
        login = (Button) findViewById(R.id.login);
        goRegister = (Button) findViewById(R.id.go_register);

        remark.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    Toast.makeText(LoginActivity.this, "记住", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(LoginActivity.this, "不记住", Toast.LENGTH_SHORT).show();
                }
            }
        });

        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.login();
            }
        });
    }

    @Override
    public void initData() {
        mPresenter = new LoginPresenter(new LoginModel(),this);
    }

    @Override
    public void showToast(String msg) {
        Toast.makeText(this, msg+"登录成功!", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }

    @Override
    public String getName() {
        return name.getText().toString();
    }

    @Override
    public String getPwd() {
        return pwd.getText().toString();
    }

    /**
     * 登录成功的逻辑
     * 在sp存入登录的用户
     * 然后跳转到主页面中
     * 并且把登录的文字换成图标
     */
    @Override
    public void isLoginOk(UserInfoBean loginInfoBean) {

        UserInfoBean.DataBean result = loginInfoBean.getData();
        SPUtil.getInstance().putString("userName",result.getUserName());
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }


    public void pay(View view) {
        Intent intent = new Intent(this, PayActivity.class);
        startActivity(intent);
    }

    public void register(View view) {
        RegisterActivity.launch(this);
    }
}
